<template>
    <el-row class="home" :gutter="20">
        <!-- <h1>nihao</h1> -->
        <el-col :span="8" style="margin-top: 10px">
            <el-card shadow="hover">
                <div class="user">
                    <img src="../../assets/img/user.jpg" alt="" />
                    <div class="user-info">
                        <p class="name">Admin</p>
                        <p class="role">超级管理员</p>
                    </div>
                </div>
                <div class="login-info">
                    <p>上次登录时间：<span>2022-7-11</span></p>
                    <p>上次登录地点：<span>北京</span></p>
                </div>
            </el-card>
            <el-card shadow="hover" style="margin-top: 10px" height="450px">
                <el-table :data="tableData">
                    <el-table-column v-for="(val,key) in tableLabel" :key="key" :prop="key" :label="val">
                    </el-table-column>
                </el-table>
            </el-card>
        </el-col>

        <el-col :span="16" style="margin-top: 10px">
            <div class="num">
                <el-card shadow="hover" :body-style="{ display:'flex', padding: 0 }" v-for="item in countData" :key="item.name">
                    <component :is="item.icon" class="icons" :style="{ background: item.color }" />
                    <div class="details">
                        <p class="num">{{ item.value }}</p>
                        <p class="txt">{{ item.name }}</p>
                    </div>
                </el-card>
            </div>
            <el-card style="height:400px">
                <!-- 准备一个dom元素用于存放图标 -->
                <div ref="echart" style="height:400px;width:90%"></div>
            </el-card>
        </el-col>
    </el-row>
</template>

<script setup lang="ts">
import { onMounted,reactive,ref } from 'vue'
import axios from 'axios'
import * as echarts from 'echarts'
const echart = ref<any>(null)

const tableData = ref([])
const countData = ref<ICountData[]>([])
let xOptions = reactive({
      // 图例文字颜色
      textStyle: {
        color: "#333",
      },
      grid: {
        left: "20%",
      },
      // 提示框
      tooltip: {
        trigger: "axis",
      },
      xAxis: {
        type: "category", // 类目轴
        data: [],
        axisLine: {
          lineStyle: {
            color: "#17b3a3",
          },
        },
        axisLabel: {
          interval: 0,
          color: "#333",
        },
      },
      yAxis: [
        {
          type: "value",
          axisLine: {
            lineStyle: {
              color: "#17b3a3",
            },
          },
        },
      ],
      color: ["#2ec7c9", "#b6a2de", "#5ab1ef", "#ffb980", "#d87a80", "#8d98b3"],
      series: [],
    })
// 饼状图的配置
let pieOptions = reactive(
{
      tooltip: {
        trigger: "item",
      },
      color: [
        "#0f78f4",
        "#dd536b",
        "#9462e5",
        "#a6a6a6",
        "#e1bb22",
        "#39c362",
        "#3ed1cf",
      ],
      series: [],
    }
)
let orderData = reactive({
    xData: [],
    series: []
})
let userData = reactive({
    xData: [],
    series: [],
})
let videoData = reactive({
    series: []
})

interface ICountData{
    name:string,
    value:Number,
    icon:string,
    color:string
}

onMounted(() => {
    // 获取商品数据
    axios({
        url:'http://127.0.0.1/api/home/getdata',
        method:'get'
    }).then((res) => {
        if(res.data.code == 200){
            tableData.value = res.data.data.tableData
        }
    })

    // 获取统计数据
    axios({
        url:'http://127.0.0.1/api/home/getcountdata',
        method:'get'
    }).then((res) => {
        countData.value = res.data.data
        // console.log(countData);
    })

    // 获取表格数据
    axios({
        url:'http://127.0.0.1:4523/m1/1794563-0-default/api/home/OrderData',
        method:'get'
    }).then((result) => {
        let res = result.data.data.orderData
        let userRes = result.data.data.userData
        let videoData = result.data.data.videoData
        
        const keyArray = Object.keys(res.data[0])
        const series:any = []
        keyArray.forEach((key) => {
            series.push({
                name: key,
                data: res.data.map((item:any) => item[key]),
                type: "line"
            })
        })
        orderData.series = series
        xOptions.xAxis.data = res.date
        xOptions.series = orderData.series
        // 渲染折线图
        let hEcharts = echarts.init(echart.value)
        hEcharts.setOption(xOptions)
    })
})

const tableLabel = {
    name: '课程',
    todayBuy: '今日购买',
    monthBuy: '本月购买',
    totalBuy: '总购买',
}
</script>

<style scoped lang="less">
.home {
    .user {
        display: flex;
        align-items: center;
        padding-bottom: 10px;
        border-bottom: 1px solid rgba(0, 0, 0, 0.6);
        margin-bottom: 20px;

        img {
            width: 150px;
            height: 150px;
            border-radius: 50%;
            margin-right: 40px;
        }
    }

    .login-info {
        p {
            line-height: 30px;
            font-size: 14px;
            color: #999;

            span {
                color: #666;
                margin-left: 60px;
            }
        }
    }
    .num{
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;

        .el-card{
            width: 30%;
            margin-bottom: 20px;
        }
        .icons{
            width: 80px;
            height: 80px;
            font-size: 30px;
            line-height: 80px;
            text-align: center;
            color: #666;
        }
        .details{
            margin-left: 15px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            color:rgba(0, 0, 0, 0.6);
            .num{
                font-size:30px;
                margin-bottom:10px;
            }
            .txt{
                font-size:14px;
                text-align: center;
                color:#999;
            }
        }
    }
}
</style>